# -*- coding: utf-8 -*-

"""
@Datetime: 2018/8/16
@Author: Zhang Yafei
"""
import json
import requests

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from scrapy.selector import Selector
import time

# browser = webdriver.Chrome()
#1.模拟登录知乎
# browser.get('https://www.zhihu.com/signup?next=%2F')
# time.sleep(15)
# browser.find_element_by_css_selector('.SignContainer-switch span').click()
# browser.find_element_by_css_selector('.Login-footer button').click()
# time.sleep(15)
# browser.find_element_by_css_selector('.Login-content input[name="username"]').send_keys('15735177116')
# time.sleep(3)
# browser.find_element_by_css_selector('.SignFlow-password input[name="password"]').send_keys('zyf123'+Keys.ENTER)
# browser.find_element_by_css_selector('.SignFlow button[type="submit"]').click()
# print(browser.page_source)
# t_selector = Selector(text=browser.page_source)
# print(t_selector.css('.tm-promo-price .tm-price::text').extract())
# browser.close()
#2.登录微博
# browser.get('https://weibo.com/')
# time.sleep(10)
# browser.find_element_by_css_selector('#loginname').send_keys('15735177116')
# browser.find_element_by_css_selector('.info_list.password input[type="password"]').send_keys('zyf120874')
# browser.find_element_by_css_selector('.info_list.login_btn a').click()
#3.执行js脚本实现下拉操作
# browser.get("https://www.oschina.net/blog")
# for i in range(3):
#     browser.execute_script("window.scrollTo(0, document.body.scrollHeight); var lenOfPage=document.body.scrollHeight; return lenOfPage;")
#     time.sleep(3)
# cookies = browser.get_cookies()
# # cookie = json.dumps(cookies)
# print(cookies)
# f = open('zhuhucookies.txt','w')
# f.write(cookies)
# f.close()
#4.设置Chrome不加载图片
# chrome_opt = webdriver.ChromeOptions()
# # prefs = {"profile.managed_default_content_settings.images":2}
# # chrome_opt.add_experimental_option("prefs", prefs)
# # browser = webdriver.Chrome(chrome_options=chrome_opt)
# # browser.get('https://www.taobao.com')
#5.phantomjs,无界面浏览器，多进程情况下phantomjs进程会下降很严重
# browser = webdriver.PhantomJS()
# browser.get('https://item.taobao.com/item.htm?id=575233867083&ali_refid=a3_430673_1006:1152829809:N:%E8%BF%9E%E8%A1%A3%E8%A3%99:098ca998259e1b368ea54938f636fc7b&ali_trackid=1_098ca998259e1b368ea54938f636fc7b&spm=a2e15.8261149.07626516002.13')
# browser.find_element_by_css_selector('input[name="TPL_username"]').send_keys('打不倒的张亚飞')
# browser.find_element_by_css_selector('#TPL_password_1').send_keys('zyf123'+Keys.ENTER)
#6.登录淘宝
# browser.get("https://login.taobao.com/")
# time.sleep(20)
# browser.get("https://detail.tmall.com/item.htm?spm=a230r.1.14.3.yYBVG6&id=538286972599&cm_id=140105335569ed55e27b&abbucket=15&sku_properties=10004:709990523;5919063:6536025")
# print(browser.page_source)
#7.保存cookies
# time.sleep(20)
# cookies = browser.get_cookies()
# print(type(cookies))
# jsonCookies = json.dumps(cookies) #python类型list->json字符串
# print(type(jsonCookies))
# with open('weibo_cookies.json', 'w') as f:
#     f.write(jsonCookies)
# #8.读取cookies到requests
# with open('weibo_cookies.json', 'r', encoding='utf-8') as f:
#     cookies = json.loads(f.read()) #json 字符串转化为python list
# print(cookies)
# cookie={}
# #转换dict调用
# for item in cookies:
#     cookie[item['name']]=item['value']
# print(cookie)
# cookies = requests.utils.cookiejar_from_dict(cookie, cookiejar=None, overwrite=True)
# session = requests.Session()
# session.cookies = cookies
# url='https://d.weibo.com/'
# header={
    # 'Connection':'keep-alive',
    # 'Cache-Control':'max-age=0',
    # 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    # 'Upgrade-Insecure-Requests':'1',
    # 'referer': 'https://www.zhihu.com/signup?next=%2F',
    # 'Host': 'web.im.weibo.com',
    # 'Origin': 'https://weibo.com',
    # 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
# response = session.get(url,headers=header,allow_redirects=False)
# print(response.text)
# result = session.get('https://www.zhihu.com/people/zhang-ya-fei-77-26/activities',headers=header)
# print(result.text)
# print(response.cookies)
#9.读取cookie到brwowser
# browser.get('https://www.taobao.com')
# with open('taobao_cookies.json', 'r', encoding='utf-8') as f:
#     cookies = json.loads(f.read())
# print(cookies)
# for cookie in cookies:
#     print(type(cookie))
#     browser.add_cookie(cookie)
#     print('\n')
# print(browser.get_cookies())
# browser.get('https://www.taobao.com')
# time.sleep(10)
# browser.get("https://detail.tmall.com/item.htm?spm=a230r.1.14.3.yYBVG6&id=538286972599&cm_id=140105335569ed55e27b&abbucket=15&sku_properties=10004:709990523;5919063:6536025")
# time.sleep(10)
# print(browser.page_source)
#browser.quit()
# browser.close()





